import { Component, OnInit } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { AcademyService } from 'src/app/project/service/icm/icm-base/academy/academy.service';
import { ProfessionService } from 'src/app/project/service/icm/icm-base/profession/profession.service';
import { ClassService } from 'src/app/project/service/icm/icm-base/class/class.service';
import { StudentService } from 'src/app/project/service/icm/icm-system-user/student/student.service';
import { QueryOptions } from 'src/app/project/model/common/query-options';
import { Router } from '@angular/router';
import { StatisticalAnalysisService } from 'src/app/project/service/icm/dashboard/statistical-analysis.service';
import { format } from 'date-fns';
import { ConfigurationService } from 'src/app/project/service/icm/icm-global/configuration/configuration.service';

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styles: [
    `
      .top-div {
        height: 300px;
        padding-top: 10px;
      }
      .div1-bg-color {
        background-color: rgb(73, 145, 255);
        background-image: linear-gradient(to left, rgb(54, 106, 252), rgb(73, 145, 255));
      }

      .div2-bg-color {
        background-color: rgb(104, 211, 155);
        background-image: linear-gradient(to left, rgb(68, 195, 128), rgb(104, 211, 155));
      }

      .div3-bg-color {
        background-color: rgb(241, 186, 83);
        background-image: linear-gradient(to left, rgb(235, 168, 38), rgb(241, 186, 83));
      }

      .div4-bg-color {
        background-color: rgb(239, 99, 98);
        background-image: linear-gradient(to left, rgb(227, 73, 73), rgb(239, 99, 98));
      }
    `,
  ],
})
export class DashboardComponent implements OnInit {
  constructor(
    private http: _HttpClient,
    private academyService: AcademyService,
    private professionService: ProfessionService,
    private classService: ClassService,
    private studentService: StudentService,
    private router: Router,
    private statisticalAnalysisService: StatisticalAnalysisService,
    private configurationService: ConfigurationService,
  ) {}

  /**
   * 主题
   */
  theme = 'macarons';
  /**
   * 学院数量
   */
  academyCount = 0;
  /**
   * 专业数量
   */
  professionCount = 0;
  /**
   * 班级数量
   */
  classCount = 0;
  /**
   * 学生数量
   */
  studentCount = 0;
  /**
   * 图2
   */
  optionsConsturction: any;

  /**
   * 综合成绩统计参数
   */
  consolidateParams = {
    academyId: null,
    professionId: null,
    classId: null,
    year: new Date(),
    standard: null,
    notStandard: null,
    earlyWarningDown: null,
    earlyWarningUp: null,
  };

  /**
   * 学业进展统计参数
   */
  creditRecordParams = {
    academyId: null,
    professionId: null,
    classId: null,
    grade: new Date(),
    standard: null,
    notStandard: null,
    earlyWarningDown: null,
    earlyWarningUp: null,
  };

  /**
   * 综合成绩统计图
   */
  optionsMajor: any;
  /**
   * 学业进展统计图
   */
  creditMajor: any;

  /**
   * 加载统计数量
   */
  loadCount() {
    this.academyService.count().subscribe(count => {
      this.academyCount = count;
    });
    this.professionService.count().subscribe(count => {
      this.professionCount = count;
    });
    this.classService.count().subscribe(count => {
      this.classCount = count;
    });
    this.studentService.count().subscribe(count => {
      this.studentCount = count;
    });
  }
  /**
   * 综合成绩统计情况
   */
  loadConstruction() {
    const legendData = ['达优人数', '未达标人数', '预警人数', '良好人数'];
    const seriesData = [
      {
        name: '达优',
        type: 'bar',
        data: [11],
      },
      {
        name: '未达标',
        type: 'bar',
        data: [22],
      },
      {
        name: '预警',
        type: 'bar',
        data: [33],
      },
      {
        name: '良好人数',
        type: 'bar',
        data: [44],
      },
    ];
    this.statisticalAnalysisService
      .consolidatedResultsCount(
        format(this.consolidateParams.year, 'YYYY'),
        this.consolidateParams.standard ? this.consolidateParams.standard : 90,
        this.consolidateParams.notStandard ? this.consolidateParams.notStandard : 60,
        this.consolidateParams.earlyWarningDown ? this.consolidateParams.earlyWarningDown : 61,
        this.consolidateParams.earlyWarningUp ? this.consolidateParams.earlyWarningUp : 70,
        this.consolidateParams.academyId,
        this.consolidateParams.professionId,
        this.consolidateParams.classId,
      )
      .subscribe(returnValue => {
        legendData.length = 0;
        seriesData.length = 0;
        for (const item of returnValue) {
          legendData.push(item.name);
          seriesData.push({ name: item.name, type: 'bar', data: [item.value] });
        }
        this.optionsMajor = {
          title: {
            text: `达优分：${this.consolidateParams.standard}, 达标分：${this.consolidateParams.notStandard}, 预警区间：${this.consolidateParams.earlyWarningDown} - ${this.consolidateParams.earlyWarningUp}`,
            textStyle: {
              fontSize: 16,
              color: 'black',
              fontStyle: 'normal',
              fontWeight: '700',
            },
          },
          tooltip: {
            trigger: 'item',
          },
          legend: {
            orient: 'vertical',
            right: 10,
            top: 20,
            icon: 'circle', // 图例的形状
            itemWidth: 8, // 图例的宽
            data: legendData,
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true,
          },
          xAxis: {
            type: 'value',
            boundaryGap: [0, 0.01],
          },
          yAxis: {
            type: 'category',
          },
          series: seriesData,
        };
      });
  }

  /**
   * 学业进展统计情况
   */
  loadGraduationCredits() {
    const legendData = ['达优人数', '未达标人数', '预警人数', '良好人数'];
    const seriesData = [
      {
        name: '达优',
        type: 'bar',
        data: [11],
      },
      {
        name: '未达标',
        type: 'bar',
        data: [22],
      },
      {
        name: '预警',
        type: 'bar',
        data: [33],
      },
      {
        name: '良好人数',
        type: 'bar',
        data: [44],
      },
    ];
    this.statisticalAnalysisService
      .graduationCreditCount(
        this.creditRecordParams.standard ? this.creditRecordParams.standard : 90,
        this.creditRecordParams.notStandard ? this.creditRecordParams.notStandard : 60,
        this.creditRecordParams.earlyWarningDown ? this.creditRecordParams.earlyWarningDown : 61,
        this.creditRecordParams.earlyWarningUp ? this.creditRecordParams.earlyWarningUp : 70,
        this.creditRecordParams.academyId,
        this.creditRecordParams.professionId,
        this.creditRecordParams.classId,
        format(this.creditRecordParams.grade, 'YYYY'),
      )
      .subscribe(returnValue => {
        legendData.length = 0;
        seriesData.length = 0;
        for (const item of returnValue) {
          legendData.push(item.name);
          seriesData.push({ name: item.name, type: 'bar', data: [item.value] });
        }
        this.creditMajor = {
          title: {
            text: `达优分：${this.creditRecordParams.standard}, 达标分：${this.creditRecordParams.notStandard}, 预警区间：${this.creditRecordParams.earlyWarningDown} - ${this.creditRecordParams.earlyWarningUp}`,
            textStyle: {
              fontSize: 16,
              color: 'black',
              fontStyle: 'normal',
              fontWeight: '700',
            },
          },
          tooltip: {
            trigger: 'item',
          },
          legend: {
            orient: 'vertical',
            right: 10,
            top: 20,
            icon: 'circle', // 图例的形状
            itemWidth: 8, // 图例的宽
            data: legendData,
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true,
          },
          xAxis: {
            type: 'value',
            boundaryGap: [0, 0.01],
          },
          yAxis: {
            type: 'category',
          },
          series: seriesData,
        };
      });
  }
  /**
   * 跳转到管理
   */
  goToList(number: number) {
    switch (number) {
      case 1:
        this.router.navigate(['/icm-base/academy']);
        break;
      case 2:
        this.router.navigate(['/icm-base/profession']);
        break;
      case 3:
        this.router.navigate(['/icm-base/class']);
        break;
      case 4:
        this.router.navigate(['/icm-base/student']);
        break;
      default:
        break;
    }
  }

  /**
   * 初始化规则
   */
  loadRule() {
    // 获得达优分
    this.configurationService.findById('7').subscribe(data => {
      this.consolidateParams.standard = data.value;
    });
    // 获得达标分
    this.configurationService.findById('8').subscribe(data => {
      this.consolidateParams.notStandard = data.value;
    });
    // 获得预警区间下限分
    this.configurationService.findById('9').subscribe(data => {
      this.consolidateParams.earlyWarningDown = data.value;
    });
    // 获得预警区间上限分
    this.configurationService.findById('10').subscribe(data => {
      this.consolidateParams.earlyWarningUp = data.value;
    });
    setTimeout(() => {
      this.loadConstruction();
    }, 2000);
    // 学业进展
    // 获得达优分
    this.configurationService.findById('11').subscribe(data => {
      this.creditRecordParams.standard = data.value;
    });
    // 获得达标分
    this.configurationService.findById('12').subscribe(data => {
      this.creditRecordParams.notStandard = data.value;
    });
    // 获得预警区间下限分
    this.configurationService.findById('13').subscribe(data => {
      this.creditRecordParams.earlyWarningDown = data.value;
    });
    // 获得预警区间上限分
    this.configurationService.findById('14').subscribe(data => {
      this.creditRecordParams.earlyWarningUp = data.value;
    });
    setTimeout(() => {
      this.loadGraduationCredits();
    }, 2000);
  }

  ngOnInit() {
    this.loadRule();
    this.loadCount();
  }
}
